package forms;


import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;

public class UserFormUpdate extends ActionForm{

	private static final long serialVersionUID = 1L;
	/**
	 * First we provide initial values for all of the input fields that have names matching the property names of the bean, with appropriate getters and setters.
	 */
	
	private String userId=null;
	private String email=null;
	private String password=null;
	private String password2=null;
	private String bio=null;
	
	private Pattern regexPattern;
	
	public String getUserId() {
		return userId;
	}

	public void setUserId(String userId) {
		this.userId = userId;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getPassword2() {
		return password2;
	}

	public void setPassword2(String password2) {
		this.password2 = password2;
	}

	public String getBio() {
		return bio;
	}

	public void setBio(String bio) {
		this.bio = bio;
	}

	public static long getSerialversionuid() {
		return serialVersionUID;
	}
	
	/**
	 * Reset all properties to their default values.
	 * 
	 * @param mapping
	 *            The mapping used to select this instance
	 * @param request
	 *            The servlet request we are processing
	 */
	public void reset(ActionMapping mapping, HttpServletRequest request) {
		setUserId(null);
		setEmail(null);
		setPassword(null);
		setPassword2(null);
		setBio(null);
	}

	/**
	 * Validate the input data. If validation fails, then report the errors in
	 * createevent.jsp, and specifically where <html:errors/> is located.
	 * 
	 * @param mapping
	 *            The mapping used to select this instance
	 * @param request
	 *            The servlet request we are processing
	 */
	public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {
		ActionErrors errors = new ActionErrors();
		
		//Check if email is valid
		String email = request.getParameter("email");
		if(email==null || email.trim().isEmpty()){
			//errorMessages.put("email", "Please enter a valid email");
			errors.add("email",new ActionMessage("errors.required", "Email"));
		}
		else{
			if(!validEmail(email)){
				errors.add("email",new ActionMessage("errors.email", "Email"));
			}
		}
		//Todo: Add check to avoid exploits
		
		//Check is new password is entered	
		String password = request.getParameter("password");
		String password2 = request.getParameter("password2");
		if(password.trim().length()!=0 || password2.trim().length()!=0 ) {
			System.out.println("Ser ut som du skrev inn passord =S");
			System.out.println(password.trim().length());
			System.out.println(password2.trim().length());
			System.out.println("her er passorder:"+password);
			if( password.equals(password2) ) {
				if(!validPassword(password)) {
					errors.add("password", new ActionMessage("errors.password", "Password"));
				}
			}
			else{
				errors.add("password2",new ActionMessage("errors.password2","Passwords"));	
			}		
		}
		
		return errors;
	}
	
	
	private boolean validEmail(String email) {
		regexPattern = Pattern.compile(".+@.+\\.[a-z]+");
		return findMatch(regexPattern, email);
	}
	
	/**
	 * @param password has to be between 6 - 15 characters long, and must contain at least one lower case and
	 * 				   one upper case letter. As well as one symbol and a digit.
	 * @return
	 */
	private boolean validPassword(String password) {
		regexPattern = Pattern.compile("^.*(?=.{6})(?=.*[a-z])(?=.*[A-Z])(?=.*[\\d\\W]).*$");
		return findMatch(regexPattern, password);
	}
	
	private boolean findMatch(Pattern regex, String field) {
		Matcher patternMatcher = regex.matcher(field);
		return patternMatcher.matches();
	}
	
}
